/*    This file is part of libepistle.
 *
 *    libepistle is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    libepistle is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with libepistle.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef EPISTLE_MESSAGE_H
#define EPISTLE_MESSAGE_H

#include <primo/err.h>
#include <stdint.h>

/* Note on symmetry of sending and receiving:
   A message that has been received can be send, but before you can receive
   on a Msg structure that has been sent you need to call message_recv_init()
   (and you probably want to dispose as well). */

typedef struct {
	uint32_t len;
        uint32_t len_tnsfd;
	unsigned char *buf;
	uint32_t buf_tnsfd;
	unsigned buf_freeable : 1;
} Msg;

int
msg_copy(Msg *des, const Msg *src, Err *err);

int
msg_extend(Msg *msg, uint32_t amt, Err *err);

void
msg_send_init(Msg *msg, uint32_t len, void *buf, int buf_freeable);

int
msg_send(int fd, Msg *msg, int wait, Err *err);

void
msg_recv_init(Msg *msg);

int
msg_recv(int fd, Msg *msg, uint32_t max_len, int wait, Err *err);

void
msg_dispose(Msg *msg);

int
msg_tnsfd(Msg *msg);

void
msg_reuse(Msg *msg);

#endif
